<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 思路:每个类型随机都取一个，把每个类型都添加到arr里，然后剩下的在字典arr里面取
      function generatePassword(lower, upper, number, symbol, length) {
        let sy = '!@#$%^&*(){}[]=<>/,/.';
        let arr = [];
        let str = '';
        function addStrItem(list) {
          str += list[Math.floor(Math.random() * list.length)];
        }

        if (upper) {
          let upperList = Array(26)
            .fill('')
            .map((item, index) => {
              return String.fromCharCode(index + 65);
            });
          arr.push(...upperList);
          addStrItem(upperList);
        }

        if (lower) {
          let lowerList = Array(26)
            .fill('')
            .map((item, index) => {
              return String.fromCharCode(index + 97);
            });
          arr.push(...lowerList);
          addStrItem(lowerList);
        }

        if (number) {
          let numberList = Array(10)
            .fill('')
            .map((item, index) => index);
          arr.push(...numberList);
          addStrItem(numberList);
        }

        if (symbol) {
          letsymbolList = sy.split('');
          arr.push(...letsymbolList);
          addStrItem(letsymbolList);
        }

        let length1 = length - str.length;
        while (length1) {
          addStrItem(str);
        }
        return str;
      }
    </script>
  </body>
</html>
